import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '@/components/Login'
// import Home from '@/components/Home.vue'
// import Welcome from '@/components/Welcome'
const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Login')
const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Home.vue')
const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '@/components/Welcome')

// import Users from '@/components/user/Users'
// import Auth from '@/components/power/Auth'
// import Roles from '@/components/power/Roles'
const Users = () => import(/* webpackChunkName: "users_auth_roles" */ '@/components/user/Users')
const Auth = () => import(/* webpackChunkName: "users_auth_roles" */ '@/components/power/Auth')
const Roles = () => import(/* webpackChunkName: "users_auth_roles" */ '@/components/power/Roles')

// import Category from '@/components/goods/Category'
// import Params from '@/components/goods/Params'
const Category = () => import(/* webpackChunkName: "category_params" */ '@/components/goods/Category')
const Params = () => import(/* webpackChunkName: "category_params" */ '@/components/goods/Params')

// import List from '@/components/goods/List'
// import Add from '@/components/goods/Add'
const List = () => import(/* webpackChunkName: "list_add" */ '@/components/goods/List')
const Add = () => import(/* webpackChunkName: "list_add" */ '@/components/goods/Add')

// import Orders from '@/components/order/Orders'
// import Report from '@/components/report/Report'
const Orders = () => import(/* webpackChunkName: "orders_report" */ '@/components/order/Orders')
const Report = () => import(/* webpackChunkName: "orders_report" */ '@/components/report/Report')

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/auth', component: Auth },
        { path: '/roles', component: Roles },
        { path: '/category', component: Category },
        { path: '/params', component: Params },
        { path: '/products', component: List },
        { path: '/products/add', component: Add },
        { path: '/orders', component: Orders },
        { path: '/charts/bar', component: Report }
      ]
    }
  ]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('webToken')
  if (!tokenStr) return next('/login')
  next()
})

export default router
